package cn.com.fuhuitong.base

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.entity.Error
import cn.com.fuhuitong.entity.MainBean
import cn.com.fuhuitong.popup.ConfirmCancelPopup
import cn.com.fuhuitong.utils.StatusBarUtils
import cn.com.fuhuitong.utils.ToastUtils
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPStaticUtils
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class BaseAbstractActivity : AppCompatActivity() {


    private val confirmCancelPopup: ConfirmCancelPopup by lazy {
        ConfirmCancelPopup(
            context = this,
            onConfirm = { any: Any?, action: ConfirmCancelPopup.Action? ->
                confirmCancelPopup.dismiss()
                ARouter.getInstance().build(ARouteConstance.ACTIVITY_URL_ACCESS)
                    .navigation()
                SPStaticUtils.clear()
                ActivityCollector.finishAll()
            },
            onCancel = { any: Any?, action: ConfirmCancelPopup.Action? ->
                confirmCancelPopup.dismiss()
            })
    }

    /**
     * 需要绑定的视图ID
     */
    abstract val layoutResId: Int

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.initStatusBar()
        setContentView(layoutResId)
        ActivityCollector.addActivity(this)
        ARouter.getInstance().inject(this@BaseAbstractActivity)

        EventBus.getDefault().register(this)

        if (confirmCancelPopup.isShowing) {
            confirmCancelPopup.dismiss()
        }

        this.init()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        ActivityCollector.removeActivity(this)
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onEventMainThread(eventBus: MainBean<*, *>) {
        if (eventBus.type is Error) {
            if (eventBus.type == Error.ERROR_401) {
                if (!confirmCancelPopup.isShowing) {
                    confirmCancelPopup.setMessage("登录已过期，请重新登录")
                        .setGoneCancel(true)
                        .setBackPressEnable(false)
                        .setOutSideDismiss(false)
                        .showPopupWindow()
                }
            }
        }
    }

    /**
     * 初始化操作
     * */
    private fun init() {
        this.doBeforeInit()
        this.initView()
        this.initData()
    }

    /**
     * 设置沉浸式状态栏
     * */
    private fun initStatusBar() {
//        var uiFlag = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
//                View.SYSTEM_UI_FLAG_VISIBLE
//        // 清除标识
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
//        }
//        //需要设置这个才能设置状态栏和导航栏颜色
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
//        }
//        // 设置刘海屏
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
//            window.attributes.layoutInDisplayCutoutMode =
//                WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
//        // 设置状态栏文字颜色
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
//            uiFlag = uiFlag or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
//        window.decorView.systemUiVisibility = uiFlag
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            window.statusBarColor = Color.TRANSPARENT
//        }
//        View.fitsSystemWindows = true

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            val flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
//            val flagTranslucentNavigation =
//                WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                val window: Window = window
//                val attributes: WindowManager.LayoutParams = window.getAttributes()
//                attributes.flags = attributes.flags or flagTranslucentNavigation
//                window.setAttributes(attributes)
//                getWindow().statusBarColor = Color.TRANSPARENT
//            } else {
//                val window: Window = window
//                val attributes: WindowManager.LayoutParams = window.getAttributes()
//                attributes.flags =
//                    attributes.flags or (flagTranslucentStatus or flagTranslucentNavigation)
//                window.setAttributes(attributes)
//            }
//        }

        //当FitsSystemWindows设置 true 时，会在屏幕最上方预留出状态栏高度的 padding
        StatusBarUtils.setRootViewFitsSystemWindows(this, false);
        //设置状态栏透明
        StatusBarUtils.setTranslucentStatus(this);
        //一般的手机的状态栏文字和图标都是白色的, 可如果你的应用也是纯白色的, 或导致状态栏文字看不清
        //所以如果你是这种情况,请使用以下代码, 设置状态使用深色文字图标风格, 否则你可以选择性注释掉这个if内容
        if (!StatusBarUtils.setStatusBarDarkTheme(this, true)) {
            //如果不支持设置深色风格 为了兼容总不能让状态栏白白的看不清, 于是设置一个状态栏颜色为半透明,
            //这样半透明+白=灰, 状态栏的文字能看得清
            StatusBarUtils.setStatusBarColor(this, 0x55000000)
        }
    }

    /**
     * 初始化视图和数据之前进行操作
     */
    open fun doBeforeInit() = Unit

    /**
     * 初始化视图
     */
    open fun initView() = Unit

    /**
     * 初始化数据
     */
    open fun initData() = Unit

    fun toast(message: String) {
        ToastUtils.showShort(this, message)
    }
}